<?php

namespace App\Models\Admin;

use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    protected $table = 'users';

    public $guarded = [];

    public $timestamps = true;

    /**
     * 获取列表
     * @data int $ 数据
     */
    public static function getList($data)
    {
        $sort = !isset($data['sort']) ? [] : (is_array(json_decode($data['sort'], true)) ? json_decode($data['sort'], true) : []);
        $list = self::when(isset($data['keywords']) && !empty($data['keywords']), function ($query) use ($data) {
            $query->where('name', 'like', '%' . $data['keywords'] . '%');
            $query->orWhere('email', 'like', '%' . $data['keywords'] . '%');
            return $query;
        })
            ->when($sort, function ($query) use ($sort) {
                foreach ($sort as $k => $vo) {
                    $query->orderBy($k, $vo);
                }
                return $query;
            })
            ->orderBy('id', 'desc')
            ->paginate($data['limit']);
        return $list;
    }


    /**
     * 更改状态
     * @param $data
     * @return array|string
     */
    public static function changeStatus($data)
    {
        $save_data = [
            'status' => $data['status'],
        ];
        try {
            $save = self::where(['id' => $data['id']])->update($save_data);
            return result_json(200, '成功');
        } catch (Exception $e) {
            Log::error("更改状态失败 Caught exception::", $e->getMessage(), '<br>');
            return result_json(500, '失败');
        }
    }


    /**
     * 删除
     * @data int $ 数据
     */
    public static function del($data)
    {
        try {
            self::delRelated($data['id']);
            $list = self::where('id', $data['id'])->delete();
            return result_json(200, '成功');
        } catch (Exception $e) {
            Log::error("删除用户失败 Caught exception::", $e->getMessage(), '<br>');
            return result_json(500, '失败');
        }
    }

    /**
     * 获取用户数据统计
     * @param $data
     * @return array|string
     */
    public static function getReport($data)
    {
        $where[] = ['created_at', '>=', $data['start_time']];
        $where[] = ['created_at', '<=', $data['end_time'] . " 23:59:59"];
        $list = self::where($where)->get();

        $day_list = self::getReportDayList($data['start_time'], $data['end_time']);
        if (!$day_list) return result_json(400, '时间参数错误');

        foreach ($day_list['list'] as $k => $vo) {
            $day_list_data[$k] = 0;
            if ($day_list['type'] == 'hour') {
                $date = strtotime($day_list['day'] . " {$vo}:00");
            } else {
                $date = strtotime($vo);
            }
            $tmp_type_arr = [];
            // 根据日期再从用户列表中提取数据
            foreach ($list as $vv) {
                if ($day_list['type'] == 'hour') {
                    $create_date = strtotime(date("Y-m-d H", strtotime($vv['created_at'])) . ":59:59");
                } else {
                    $create_date = strtotime(date("Y-m-d", strtotime($vv['created_at'])));
                }
                if ($create_date != $date) continue;
                $day_list_data[$k] += 1;
            }
        }
        $day_list_data = array_values($day_list_data);
        $data = [
            'day_list' => $day_list['list'],
            'day_list_data' => $day_list_data,
        ];
        return result_json(200, '成功', $data);
    }

    /**
     * 获取日期列表
     * @param $start_time
     * @param $end_time
     * @return array|string
     */
    private static function getReportDayList($start_time, $end_time)
    {
        // 获取时间列表
        $day_arr = getDateDayArr($start_time, $end_time);
        if (count($day_arr) <= 1) {
            $day = $day_arr[0];
            $day_arr = getDayHourArr($day_arr[0]);
            $type = 'hour';
        } else {
            $type = 'date';
        }

        return ['list' => $day_arr, 'type' => $type, 'day' => isset($day) ? $day : ''];
    }
}